#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
调试标题结构信息
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from src.processors.docx_processor import DOCXProcessor
from src.core.text_cleaner import TextCleaner
import json

def debug_heading_structure():
    """调试标题结构信息"""
    
    print("=== 调试标题结构信息 ===")
    
    # 测试文档路径
    test_file = "e:\\trae\\dify\\data\\面向水库风险监测的低成本低功耗边缘智能系统.docx"
    
    if not os.path.exists(test_file):
        print(f"测试文件不存在: {test_file}")
        return
    
    # 1. 处理文档
    print("\n1. 处理DOCX文档...")
    processor = DOCXProcessor()
    
    with open(test_file, 'rb') as f:
        file_content = f.read()
    
    result = processor.extract_content(file_content)
    text = result['text']
    structure = result.get('structure', {})
    
    print(f"文档文本长度: {len(text)} 字符")
    print(f"结构信息键: {list(structure.keys()) if structure else '无'}")
    
    # 2. 检查标题信息
    if 'headings' in structure:
        headings = structure['headings']
        print(f"\n2. 标题信息分析 (共{len(headings)}个标题):")
        
        for i, heading in enumerate(headings[:10]):  # 只显示前10个
            print(f"\n标题 {i+1}:")
            print(f"  完整信息: {heading}")
            
            # 检查可能的位置字段
            position_fields = ['start_pos', 'line_number', 'line_num', 'position', 'start', 'offset']
            for field in position_fields:
                if field in heading:
                    print(f"  位置字段 '{field}': {heading[field]}")
    
    # 3. 检查文本前1000字符
    print(f"\n3. 文本前1000字符:")
    print(repr(text[:1000]))
    
    # 4. 保存调试信息
    debug_info = {
        'text_length': len(text),
        'text_preview': text[:1000],
        'structure_keys': list(structure.keys()) if structure else [],
        'headings': structure.get('headings', [])[:10] if structure else []
    }
    
    with open('debug_heading_structure_result.json', 'w', encoding='utf-8') as f:
        json.dump(debug_info, f, ensure_ascii=False, indent=2)
    
    print(f"\n调试信息已保存到: debug_heading_structure_result.json")

if __name__ == "__main__":
    debug_heading_structure()